package com.git.hui.yinshua.biz.user.service.convert;

import com.git.hui.yinshua.api.model.constants.GlobalConstants;
import com.git.hui.yinshua.api.model.permission.UserRole;
import com.git.hui.yinshua.biz.rest.models.user.res.UserAddressDto;
import com.git.hui.yinshua.biz.rest.models.user.res.UserInfoDto;
import com.git.hui.yinshua.biz.user.dao.domain.UserAddressEntity;
import com.git.hui.yinshua.biz.user.dao.domain.UserEntity;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author YiHui
 * @date 2024/6/11
 */
public class UserConvert {

    public static UserAddressDto toAddress(UserAddressEntity entity) {
        UserAddressDto dto = new UserAddressDto();
        dto.setAddressId(entity.getId())
                .setAddress(entity.getReceivingAddress())
                .setArea(entity.getReceivingArea())
                .setPhone(entity.getReceivingPhone())
                .setPerson(entity.getReceivingPerson())
                .setTag(entity.getReceivingTag())
                .setDefaultAddress(Objects.equals(entity.getMainAddress(), GlobalConstants.AddressApi.MAIN_ADDRESS));
        return dto;
    }

    public static List<UserAddressDto> batchToAddress(List<UserAddressEntity> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }

        return list.stream().map(UserConvert::toAddress).collect(Collectors.toList());
    }

    public static UserInfoDto toUser(UserEntity entity) {
        UserInfoDto dto = new UserInfoDto();
        dto.setUserId(entity.getId());
        dto.setUserName(entity.getUname());
        dto.setAvatar(entity.getAvatar());
        dto.setRole(UserRole.roleOf(entity.getRole()).name());
        dto.setNickName(entity.getNickname());
        dto.setWxId(entity.getWxId());
        dto.setLoginAt(entity.getLoginAt());
        dto.setSource(entity.getSource());
        dto.setPhone(entity.getPhone());
        return dto;
    }

    public static List<UserInfoDto> batchToUser(List<UserEntity> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }

        return list.stream().map(UserConvert::toUser).collect(Collectors.toList());
    }

}
